package com.epwk.driver;


import com.epwk.element.WebEles;
import com.epwk.logger.LoggerControler;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

/**
 * Created by Administrator on 2016/11/13.
 *
 * 封装driver，便于调用
 */
public class SeleniumDriver {

    public static WebDriver driver;

    final static LoggerControler log= LoggerControler.getLogger(SeleniumDriver.class);

    public static WebDriver openBrowser(String browser,String url){
//        获取项目的根目录
        String path = System.getProperty("user.dir");

        if (browser.equals("chrome")){
            System.setProperty("webdriver.chrome.driver",path+"/driver/chromedriver.exe");
            driver = new ChromeDriver();
            log.info("打开chrome");
        }else if (browser.equals("ie")){
            System.setProperty("webdriver.ie.driver",path+"/driver/IEDriverServer.exe");
            driver = new InternetExplorerDriver();
            log.info("打开ie");
        }else if (browser.equals("firefox")){
            driver = new FirefoxDriver();
            log.info("打开Firefox");
        }else {
            log.info("传入的浏览器名称有误");
        }
        driver.get(url);
        return driver;

    }

    public static WebDriver openBrowser(String browser){
//        获取项目的根目录
        String path = System.getProperty("user.dir");

        if (browser.equals("chrome")){
            System.setProperty("webdriver.chrome.driver",path+"/driver/chromedriver.exe");
            driver = new ChromeDriver();
            log.info("打开chrome");
        }else if (browser.equals("ie")){
            System.setProperty("webdriver.ie.driver",path+"/driver/IEDriverServer.exe");
            driver = new InternetExplorerDriver();
            log.info("打开ie");
        }else if (browser.equals("firefox")){
            driver = new FirefoxDriver();
            log.info("打开Firefox");
        }else {
            log.info("传入的浏览器名称有误");
        }
        return driver;

    }

    public static void backBrowser(){
        driver.navigate().back();
        log.info("页面后退");
    }

    public static void forwardBrowser(){
        driver.navigate().forward();
        log.info("页面前进");
    }

    public static void refreshBrowser(){
        driver.navigate().refresh();
        log.info("页面刷新");
    }

    public static void maxBrowser(){
        driver.manage().window().maximize();
        log.info("页面最大化");
    }

    public static void sizeBrowser(int x,int y){
        Dimension dimension = new Dimension(x,y);
        driver.manage().window().setSize(dimension);
        log.info("页面大小:("+x+","+y+")");
    }

    public static void closeBrowser(){
        driver.quit();
        log.info("关闭浏览器");
    }

    public static String getUrlBrowser(){
        String url = driver.getCurrentUrl();
        log.info("获取当前url");
        return url;
    }

    public static String getTitleBrowser(){
        String title = driver.getTitle();
        log.info("获取页面Title");
        return title;
    }

}
